package com.xnyzc.lhy.mis.service.impl.cms.auditentry;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xnyzc.lhy.common.component.sms.AliCloudSMSUtils;
import com.xnyzc.lhy.common.constant.AliCloudSmsConstant;
import com.xnyzc.lhy.common.constant.CommonConstant;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.*;
import com.xnyzc.lhy.common.util.validation.RegularValidation;
import com.xnyzc.lhy.mis.entity.area.OaSysArea;
import com.xnyzc.lhy.mis.entity.cms.auditentry.OaSysAuditEntry;
import com.xnyzc.lhy.mis.entity.dictionaries.OaSysDict;
import com.xnyzc.lhy.mis.entity.system.OaSysGlobalConfig;
import com.xnyzc.lhy.mis.entity.user.OaDUser;
import com.xnyzc.lhy.mis.entity.user.OaDUserInfo;
import com.xnyzc.lhy.mis.entity.vehicle.OaSysVehicle;
import com.xnyzc.lhy.mis.entity.vehicle.OaSysVehicleMdict;
import com.xnyzc.lhy.mis.mapper.area.OaSysAreaMapper;
import com.xnyzc.lhy.mis.mapper.cms.auditentry.OaSysAuditEntryMapper;
import com.xnyzc.lhy.mis.mapper.cms.sms.OaSmsCodeRecordMapper;
import com.xnyzc.lhy.mis.mapper.cms.sms.OaSmsErrorCodeMapper;
import com.xnyzc.lhy.mis.mapper.dictionaries.OaSysDictMapper;
import com.xnyzc.lhy.mis.mapper.system.OaSysGlobalConfigMapper;
import com.xnyzc.lhy.mis.mapper.system.OaSysUserMapper;
import com.xnyzc.lhy.mis.mapper.user.OaDUserInfoMapper;
import com.xnyzc.lhy.mis.mapper.user.OaDUserMapper;
import com.xnyzc.lhy.mis.mapper.vehicle.OaSysVehicleMapper;
import com.xnyzc.lhy.mis.mapper.vehicle.OaSysVehicleMdictMapper;
import com.xnyzc.lhy.mis.service.cms.auditentry.H5AuditEntryService;
import com.xnyzc.lhy.order.entity.sms.OaSmsCodeRecord;
import com.xnyzc.lhy.order.entity.sms.OaSmsErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * H5审核入驻接口实现类
 *
 * @author zhaolijie
 * @date
 */
@Slf4j
@Service
public class H5AuditEntryServiceImpl implements H5AuditEntryService {


    @Autowired
    private OaDUserMapper oaDUserMapper;

    @Autowired
    private OaSysAuditEntryMapper oaSysAuditEntryMapper;

    @Autowired
    private OaSysGlobalConfigMapper oaSysGlobalConfigMapper;

    @Autowired
    private OaSmsErrorCodeMapper oaSmsErrorCodeMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private OaSmsCodeRecordMapper oaSmsCodeRecordMapper;

    @Autowired
    private OaSysUserMapper oaSysUserMapper;

    @Value("${global.whitelist.oaDUserPhone}")
    private String whitelistOaDUserPhone;

    /**
     * 用户类型（0：用户、1：司机、2：管理员）
     */
    private Integer userType = 1;
    /**
     * 用户盐
     */
    private String tag = "lhy";
    /**
     * /验证码类型（0：注册、1：登录、2：忘记密码、3：修改手机号、4：推荐注册 5：绑定银行卡）
     */
    private Integer codeType = 0;

    @Autowired
    private OaSysVehicleMdictMapper oaSysVehicleMdictMapper;

    @Autowired
    private OaSysDictMapper oaSysDictMapper;

    @Autowired
    private OaSysAreaMapper oaSysAreaMapper;

    @Autowired
    private OaSysVehicleMapper oaSysVehicleMapper;

    @Autowired
    private OaDUserInfoMapper oaDUserInfoMapper;


    /**
     * H5审核入驻获取验证码
     *
     * @param oaSmsCodeRecord：
     * @return
     */
    @Override
    public Rv getH5SmsCode(OaSmsCodeRecord oaSmsCodeRecord) {
        String phone = oaSmsCodeRecord.getPhone();
        // 判空
        if (CheckUtil.strIsEmpty(phone)) {
            throw PangException.create(EErrorCode.missingArg);
        }
        // 正则检测手机号是否正确
        if (RegularValidation.validationData(RegularValidation.CASE_PHONE, phone) == false) {
            throw PangException.create(EErrorCode.dataCheckErr);
        }

        // 查询该手机号在司机表是否被注册
        QueryWrapper<OaDUser> oaDUserQueryWrapper = new QueryWrapper<>();
        oaDUserQueryWrapper.eq(OaDUser.DRIVER_PHONE, phone);
        Integer selectCountOaDUser = oaDUserMapper.selectCount(oaDUserQueryWrapper);
        if (selectCountOaDUser > 0) {
            throw PangException.create(EErrorCode.phoneRegister);
        }

        // 查询该手机号在审核入驻表是否未审核或者审核通过
        QueryWrapper<OaSysAuditEntry> auditEntryQueryWrapper = new QueryWrapper<>();
        auditEntryQueryWrapper.eq(OaSysAuditEntry.STATUS, 0)
                .eq(OaSysAuditEntry.DRIVER_PHONE, phone);
        Integer selectCountAuditEntry = oaSysAuditEntryMapper.selectCount(auditEntryQueryWrapper);
        if (selectCountAuditEntry > 0) {
            throw PangException.create(EErrorCode.iphoneAudit);
        }

        // 查询系统配置
        QueryWrapper<OaSysGlobalConfig> oaSysGlobalConfigQueryWrapper = new QueryWrapper<>();
        List<OaSysGlobalConfig> oaSysGlobalConfigs = oaSysGlobalConfigMapper.selectList(oaSysGlobalConfigQueryWrapper);
        if (CheckUtil.objIsEmpty(oaSysGlobalConfigs)) {
            throw PangException.create(EErrorCode.noData);
        }

        //判断错误次数
        OaSysGlobalConfig oaSysGlobalConfig = oaSysGlobalConfigs.get(0);
        Boolean checkErrorCount = this.getCheckErrorCount(oaSysGlobalConfig, phone, userType, tag);
        if (!checkErrorCount) {
            throw PangException.create(EErrorCode.exceedLimit);
        }

        //短信验证码获取控制过期时间
        Integer smsLimitExpireTime = CommonConstant.REDIS_REDIS_SMS_LIMIT_EXPIRE_TIME;
        if (!CheckUtil.objIsEmpty(oaSysGlobalConfig.getCodeValidTime())) {
            smsLimitExpireTime = oaSysGlobalConfig.getCodeValidTime();
        }

        // 验证码key
        String smsCodeKey = CommonConstant.REDIS_SMS_LIMIT + "_" + tag + "_" + userType + "_" + phone;
        if (redisTemplate.hasKey(smsCodeKey)) {
            // 删除key
            redisTemplate.delete(smsCodeKey);
        }

        // 获取4位数验证码
        String code = RandomUtil.getRandomNumberStr(4);
        redisTemplate.opsForValue().set(smsCodeKey, code, ConvertUtil.toLong(smsLimitExpireTime), TimeUnit.SECONDS);
        // 发送验证码
        JSONObject smsJson = new JSONObject();
        smsJson.put("code", code);
        try {
            //QCloudSMSUtils.sendMessage(phone, QCloudSmsConstant.VERIFICATION_CODE, smsJson);
            AliCloudSMSUtils.sendMessage(phone, AliCloudSmsConstant.VERIFICATION_CODE, smsJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.saveCode(phone, code, codeType, userType, tag);
        return Rv.wrap(EErrorCode.success);
    }

    /**
     * 效验验证码
     *
     * @param oaSmsCodeRecord
     * @return
     */
    @Override
    public Rv validationH5SmsCode(OaSmsCodeRecord oaSmsCodeRecord) {
        this.checkSmsCode(String.valueOf(userType), oaSmsCodeRecord.getPhone(), oaSmsCodeRecord.getCode(), tag, whitelistOaDUserPhone);
        return Rv.wrap(EErrorCode.success);
    }

    /**
     * 获取字典车辆信息
     *
     * @param level：级别
     * @param parentId：父ID
     * @return
     */
    @Override
    public Rv getVehicleInfo(String level, String parentId, String name) {
        if (CheckUtil.objIsEmpty(level, parentId)) {
            throw PangException.create(EErrorCode.missingArg);
        }
        // 查询车辆字典表的信息
        QueryWrapper<OaSysVehicleMdict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaSysVehicleMdict.PARENT_ID, parentId);
        if (CheckUtil.objIsNotEmpty(name)) {
            queryWrapper.like(OaSysVehicleMdict.NAME, name);
        }
        if ("1".equals(level)) {
            queryWrapper.eq(OaSysVehicleMdict.EXPAND1, level);
        }
        List<OaSysVehicleMdict> oaSysVehicleMdicts = oaSysVehicleMdictMapper.selectList(queryWrapper);
        if ("2".equals(level)) {
            List<Long> listIn = new ArrayList<>();
            for (int i = 0; i < oaSysVehicleMdicts.size(); i++) {
                listIn.add(oaSysVehicleMdicts.get(i).getMdictId());
            }
            QueryWrapper<OaSysVehicleMdict> queryWrapperIn = new QueryWrapper<>();
            queryWrapperIn.in(OaSysVehicleMdict.PARENT_ID, listIn);
            List<OaSysVehicleMdict> oaSysVehicleMdictsIn = oaSysVehicleMdictMapper.selectList(queryWrapperIn);
            for (int i = 0; i < oaSysVehicleMdictsIn.size(); i++) {
                oaSysVehicleMdicts.add(oaSysVehicleMdictsIn.get(i));
            }
            List<Map<String, Object>> menuMapList = ListToTreeUtil.getVehicleMdictList(oaSysVehicleMdicts);
            JSONArray menuResultList = ListToTreeUtil.listToTree(JSONArray.parseArray(JSON.toJSONString(menuMapList)), "id", "pId", "children");
            return Rv.wrap(menuResultList);
        }
        return Rv.wrap(oaSysVehicleMdicts);
    }

    /**
     * 获取系统字典信息
     *
     * @return
     */
    @Override
    public Rv getSysDict() {
        // 查询颜色
        QueryWrapper<OaSysDict> queryWrapperColorType = new QueryWrapper<>();
        queryWrapperColorType.eq(OaSysDict.TYPE, "vegicle_color_type");
        List<OaSysDict> oaSysDictsColor = oaSysDictMapper.selectList(queryWrapperColorType);

        // 查询⻋辆燃料类型
        QueryWrapper<OaSysDict> queryWrapperFuelType = new QueryWrapper<>();
        queryWrapperFuelType.eq(OaSysDict.TYPE, "fuel_type");
        List<OaSysDict> oaSysDictsFuelType = oaSysDictMapper.selectList(queryWrapperFuelType);

        // 查询用车类型
        QueryWrapper<OaSysDict> queryWrapperRequireLevel = new QueryWrapper<>();
        queryWrapperRequireLevel.eq(OaSysDict.TYPE, "require_level");
        List<OaSysDict> oaSysDictsRequireLevel = oaSysDictMapper.selectList(queryWrapperRequireLevel);

        JSONObject jsonObjectResult = new JSONObject();
        jsonObjectResult.put("colorType", oaSysDictsColor);
        jsonObjectResult.put("fuelType", oaSysDictsFuelType);
        jsonObjectResult.put("requireLevel", oaSysDictsRequireLevel);
        return Rv.wrap(jsonObjectResult);
    }

    /**
     * 添加入驻信息
     *
     * @param oaSysAuditEntry
     * @return
     */
    @Override
    public Rv insertAuditEntryInfo(OaSysAuditEntry oaSysAuditEntry) {
        //手机号
        String driverPhone = oaSysAuditEntry.getDriverPhone();
        // 姓名
        String driverName = oaSysAuditEntry.getDriverName();
        // 身份证号
        String idcard = oaSysAuditEntry.getIdcard();
        //性别
        Integer sex = oaSysAuditEntry.getSex();
        // 驾龄
        String driveAge = oaSysAuditEntry.getDriveAge();
        // 车牌号
        String carNo = oaSysAuditEntry.getCarNo();
        // 颜色
        Integer color = oaSysAuditEntry.getColor();
        //车系编码
        String vehCode = oaSysAuditEntry.getVehCode();
        //部门编码-用于对应供应商
        Integer officeCode = oaSysAuditEntry.getOfficeCode();
        // 城市编码
        String cityCode = oaSysAuditEntry.getCityCode();
        // 网约车运输证
        Long netCarCertificate = oaSysAuditEntry.getNetCarCertificate();
        // 网约车驾驶员证
        Long netDriverCertificate = oaSysAuditEntry.getNetDriverCertificate();
        // 燃油类型
        Integer engine = oaSysAuditEntry.getEngine();
        if (CheckUtil.objIsEmpty(
                driverPhone,
                driverName,
                idcard,
                sex,
                driveAge,
                carNo,
                color,
                vehCode,
                officeCode,
                cityCode,
                netCarCertificate,
                netDriverCertificate,
                engine
        )) {
            throw PangException.create(EErrorCode.missingArg);
        }
        if (officeCode < 1) {
            throw PangException.create(EErrorCode.missingArg);
        }
        // 手机号验证
        if (RegularValidation.validationData(RegularValidation.CASE_PHONE, driverPhone) == false) {
            throw PangException.create(EErrorCode.errorPhone);
        }
        // 姓名验证
        if (RegularValidation.validationData(RegularValidation.CASE_NAME, driverName) == false) {
            throw PangException.create(EErrorCode.errorDriverName);
        }
        // 身份证号验证
        if (RegularValidation.validationData(RegularValidation.CASE_ID_CARD, idcard) == false) {
            throw PangException.create(EErrorCode.errorIcCard);
        }
        // 性别验证
        if (RegularValidation.validationData(RegularValidation.CASE_POSITIVE_INTEGERZERO, String.valueOf(sex)) == false) {
            throw PangException.create(EErrorCode.errorSex);
        }
        // 车牌号验证
        if (String.valueOf(carNo).length() == 7) {
            if (RegularValidation.validationData(RegularValidation.CASE_VEHNO, String.valueOf(carNo)) == false) {
                throw PangException.create(EErrorCode.errorCarNo);
            }
        } else if (String.valueOf(carNo).length() == 8) {
            if (RegularValidation.validationData(RegularValidation.CASE_NEWVEHNO, String.valueOf(carNo)) == false) {
                throw PangException.create(EErrorCode.errorCarNo);
            }
        }

        // 查询该手机号在司机表是否被注册
        QueryWrapper<OaDUser> oaDUserQueryWrapper = new QueryWrapper<>();
        oaDUserQueryWrapper.eq(OaDUser.DRIVER_PHONE, driverPhone);
        Integer selectCountOaDUser = oaDUserMapper.selectCount(oaDUserQueryWrapper);
        if (selectCountOaDUser > 0) {
            throw PangException.create(EErrorCode.phoneRegister);
        }

        // 身份证号是否被注册
        QueryWrapper<OaDUserInfo> oaDUserInfoQueryWrapper = new QueryWrapper<>();
        oaDUserInfoQueryWrapper.eq(OaDUserInfo.IDENTITY_NUMBER, idcard);
        Integer oaDUserInfoSelectCount = oaDUserInfoMapper.selectCount(oaDUserInfoQueryWrapper);
        if (oaDUserInfoSelectCount > 0) {
            throw PangException.create(EErrorCode.idCardRepeat);
        }

        // 查询车牌还是否被注册
        QueryWrapper<OaSysVehicle> oaSysVehicleQueryWrapper = new QueryWrapper<>();
        oaSysVehicleQueryWrapper.eq(OaSysVehicle.VEH_NO, carNo);
        Integer oaSysVehicleCount = oaSysVehicleMapper.selectCount(oaSysVehicleQueryWrapper);
        if (oaSysVehicleCount > 0) {
            throw PangException.create(EErrorCode.vehNoRepeat);
        }

        // 查询该手机号在审核入驻表是否未审核或者审核通过
        QueryWrapper<OaSysAuditEntry> auditEntryQueryWrapperPhone = new QueryWrapper<>();
        auditEntryQueryWrapperPhone.eq(OaSysAuditEntry.STATUS, 0)
                .eq(OaSysAuditEntry.DRIVER_PHONE, driverPhone);
        Integer selectCountAuditEntryPhone = oaSysAuditEntryMapper.selectCount(auditEntryQueryWrapperPhone);
        if (selectCountAuditEntryPhone > 0) {
            throw PangException.create(EErrorCode.iphoneAudit);
        }

        // 查询该身份证号在审核入驻表是否未审核或者审核通过
        QueryWrapper<OaSysAuditEntry> auditEntryQueryWrapperIDCard = new QueryWrapper<>();
        auditEntryQueryWrapperIDCard.eq(OaSysAuditEntry.STATUS, 0)
                .eq(OaSysAuditEntry.IDCARD, idcard);
        Integer selectCountAuditEntryIDCard = oaSysAuditEntryMapper.selectCount(auditEntryQueryWrapperIDCard);
        if (selectCountAuditEntryIDCard > 0) {
            throw PangException.create(EErrorCode.idCardAudit);
        }

        // 查询该车牌号在审核入驻表是否未审核或者审核通过
        QueryWrapper<OaSysAuditEntry> auditEntryQueryWrapperVehNo = new QueryWrapper<>();
        auditEntryQueryWrapperVehNo.eq(OaSysAuditEntry.STATUS, 0)
                .eq(OaSysAuditEntry.CAR_NO, carNo);
        Integer selectCountAuditEntryVehNo = oaSysAuditEntryMapper.selectCount(auditEntryQueryWrapperVehNo);
        if (selectCountAuditEntryVehNo > 0) {
            throw PangException.create(EErrorCode.vehNoCardAudit);
        }


        long nextId = IDUtil.nextId();
        LocalDateTime ldt = LocalDateTime.now();
        String format = ldt.format(DateTimeFormatter.BASIC_ISO_DATE);
        String residentSerialNumber = "RZ" + format + RandomUtil.getRandomNumberStr(4);
        oaSysAuditEntry.setResidentSerialNumber(residentSerialNumber);
        oaSysAuditEntry.setAuditId(nextId);
        oaSysAuditEntry.setStatus(0);
        oaSysAuditEntry.setCreateUser(nextId);
        oaSysAuditEntry.setTag(tag);
        oaSysAuditEntry.setCreateTime(new Date());

        int insert = oaSysAuditEntryMapper.insert(oaSysAuditEntry);
        if (insert > 0) {
            return Rv.wrap(EErrorCode.success);
        } else {
            return Rv.wrap(EErrorCode.addError);
        }
    }

    /**
     * 获取城市信息
     *
     * @return
     */
    @Override
    public Rv getCityInfo(String name) {
        QueryWrapper<OaSysArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaSysArea.TYPE, 3);
        if (CheckUtil.strIsNotEmpty(name)) {
            queryWrapper.like(OaSysArea.NAME, name);
        }
        List<OaSysArea> oaSysAreas = oaSysAreaMapper.selectList(queryWrapper);
        return Rv.wrap(oaSysAreas);
    }

    /**
     * 效验验证码
     *
     * @param userType
     * @param phone
     * @param code
     * @param tag
     * @param whitelist
     */
    public void checkSmsCode(String userType, String phone, String code, String tag, String whitelist) {
        // 验证手机号是否是白名单
        String[] phoneList = whitelist.split(",");
        if (!CheckUtil.arrayIsEmpty(phoneList)) {
            for (String p : phoneList) {
                if (Objects.equals(phone, p)) {
                    return;
                }
            }
        }

        // 验证码key
        String smsCodeKey = CommonConstant.REDIS_SMS_LIMIT + "_" + tag + "_" + userType + "_" + phone;
        Date date = new Date();
        log.info("redis中验证码key：" + smsCodeKey, "用户输入验证码：" + code + ",:" + redisTemplate.hasKey(smsCodeKey));
        if (!redisTemplate.hasKey(smsCodeKey)) {
            // 记录错误
            recordSmsErrorCode(userType, phone, tag, smsCodeKey, date);
            //throw PangException.create(EErrorCode.verifCodeNotHas);
        }

        log.info("验证码code：" + (String) redisTemplate.opsForValue().get(smsCodeKey));
        if (!Objects.equals(code, (String) redisTemplate.opsForValue().get(smsCodeKey))) {
            recordSmsErrorCode(userType, phone, tag, smsCodeKey, date);
            //return;
        }

        QueryWrapper<OaSmsCodeRecord> oaSmsCodeRecordQueryWrapperVerification = new QueryWrapper<>();
        oaSmsCodeRecordQueryWrapperVerification.eq(OaSmsCodeRecord.PHONE, phone)
                .eq(OaSmsCodeRecord.TAG, tag)
                .eq(OaSmsCodeRecord.CODE, code)
                .eq(OaSmsCodeRecord.IS_USE, 1);
        Integer selectCount = oaSmsCodeRecordMapper.selectCount(oaSmsCodeRecordQueryWrapperVerification);
        if (selectCount > 0) {
            throw PangException.create(EErrorCode.verifCodeNotHas);
        }


        redisTemplate.delete(smsCodeKey);
        // 更新验证码为已使用
        OaSmsCodeRecord oaSmsCodeRecord = new OaSmsCodeRecord();
        oaSmsCodeRecord.setIsUse(1);
        oaSmsCodeRecord.setUpdateTime(date);

        QueryWrapper<OaSmsCodeRecord> oaSmsCodeRecordQueryWrapper = new QueryWrapper<>();
        oaSmsCodeRecordQueryWrapper.eq(OaSmsCodeRecord.PHONE, phone)
                .eq(OaSmsCodeRecord.TAG, tag)
                .eq(OaSmsCodeRecord.CODE, code);
        oaSmsCodeRecordMapper.update(oaSmsCodeRecord, oaSmsCodeRecordQueryWrapper);
    }

    /**
     * 记录错误
     *
     * @param userType
     * @param phone
     * @param tag
     * @param smsCodeKey
     * @param date
     */
    private void recordSmsErrorCode(String userType, String phone, String tag, String smsCodeKey, Date date) {
        // 记录错误
        OaSmsErrorCode oaSmsErrorCode = new OaSmsErrorCode();
        Long id = IDUtil.nextId();
        oaSmsErrorCode.setErrorCodeId(id);
        oaSmsErrorCode.setPhone(phone);
        oaSmsErrorCode.setErrorTime(LocalDate.now());
        oaSmsErrorCode.setTag(tag);
        oaSmsErrorCode.setUserType(ConvertUtil.toInt(userType));
        oaSmsErrorCode.setCreateTime(date);
        oaSmsErrorCode.setCreateUser(id);
        oaSmsErrorCode.setUpdateTime(date);
        oaSmsErrorCode.setUpdateUser(id);
        oaSmsErrorCodeMapper.insert(oaSmsErrorCode);
        // redisTemplate.delete(smsCodeKey);
        throw PangException.create(EErrorCode.verifCodeError);
    }

    /**
     * 保存验证码
     *
     * @param phone
     * @param code
     * @param codeType
     * @param userType
     * @param tag
     */
    private void saveCode(String phone, String code, Integer codeType, Integer userType, String tag) {
        OaSmsCodeRecord oaSmsCodeRecord = new OaSmsCodeRecord();
        Long id = IDUtil.nextId();
        Date date = new Date();
        oaSmsCodeRecord.setRecordId(id);
        oaSmsCodeRecord.setPhone(phone);
        oaSmsCodeRecord.setCodeType(codeType);
        oaSmsCodeRecord.setCode(code);
        oaSmsCodeRecord.setUserType(userType);
        oaSmsCodeRecord.setTag(tag);
        oaSmsCodeRecord.setCreateTime(date);
        oaSmsCodeRecord.setCreateUser(id);
        oaSmsCodeRecord.setUpdateUser(id);
        oaSmsCodeRecord.setUpdateTime(date);
        oaSmsCodeRecordMapper.insert(oaSmsCodeRecord);
    }

    /**
     * 获取用户错误次数
     *
     * @param oaSysGlobalConfig
     * @param phone
     * @param userType
     * @param tag
     * @return
     */
    private Boolean getCheckErrorCount(OaSysGlobalConfig oaSysGlobalConfig, String phone, Integer userType, String tag) {
        // 查询当日的错误次数
        QueryWrapper<OaSmsErrorCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaSmsErrorCode.TAG, tag);
        queryWrapper.eq(OaSmsErrorCode.PHONE, phone);
        queryWrapper.eq(OaSmsErrorCode.USER_TYPE, userType);
        queryWrapper.eq(OaSmsErrorCode.ERROR_TIME, DateUtil.getDateDay());
        Integer count = oaSmsErrorCodeMapper.selectCount(queryWrapper);

        if (CheckUtil.objIsEmpty(count)) {
            return true;
        }

        Integer errorNum = CommonConstant.REDIS_SMS_CODE_ERROR_NUM;
        if (!CheckUtil.objIsEmpty(oaSysGlobalConfig.getCodeErrorCount())) {
            errorNum = oaSysGlobalConfig.getCodeErrorCount();
        }
        // 是否超过错误限制
        if (count > errorNum) {
            return false;
        }
        return true;
    }

    @Override
    public Rv getDriverManagersByCity(String cityId) {
        if (CheckUtil.strIsEmpty(cityId)) {
            return Rv.error(EErrorCode.noData);
        }
        Long roleId = 1768999788642304L;//在系统中创建好的供应商角色对应的ID
        List<Map<String, Object>> managers = oaSysUserMapper.getDriverManagersByCityId(roleId, cityId);
        return Rv.wrap(managers);
    }
}
